<template>
	<view class="content">
		<view class="uni-margin-wrap">
			<swiper class="swiper" circular :indicator-dots="indicatorDots" :autoplay="autoplay" :interval="interval"
				:duration="duration">
				<swiper-item v-for="(item,index) in swiperList" :key="index">
					<view class="swiper-item">
						<image mode="aspectFit" :src="item"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		<view class="wrap">
			<!-- 本地服务 -->
			<block v-if="localData.length">
				<local :localData="localData"></local>
			</block>
			<u-line></u-line>
			<!-- 去领养、送养、购买 -->
			<checktab></checktab>
			<u-line></u-line>
			<!-- 推荐 -->
			<recommend></recommend>
			<!-- @onSwiperClick="onSwiperClick" -->
		</view>
	</view>
</template>

<script>
	import local from "../../../components/localservice/localservice.vue"
	import checktab from "../../../components/checktab/checktab.vue"
	import recommend from "../../../components/recommend/recommend.vue"
	export default {
		data() {
			return {
				indicatorDots: false,
				swiperList: [
					"../../../static/banner/1.jpg",
					"../../../static/banner/2.jpg",
					"../../../static/banner/3.jpg"
				],
				autoplay: true,
				interval: 2000,
				duration: 500,
				localData: [], // 本地服务
				// 用户每天可用的体验次数
				dailyQuota: 2,
				// 当前用户的剩余体验次数
				quotaLeft: null,
			};
		},
		onLoad() {
			// this.$db.collection('swiperList').where({}).get().then(res => {
			// 	this.swiperList = res.data
			// 	// console.log(res.data, 'client')
			// })
			this.getPet() // 本地服务
			this.checkAndResetQuota(); // 拨打电话次数
			this.checkReg()
		},
		methods: {
			getPet() {
				wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'getLostPet',
						limit: 2,
					}
				}).then(res => {
					this.localData = res.result.data
				})
			},
			// 检查并可能重置体验次数
			checkAndResetQuota() {
				// 从localStorage读取上一次访问的时间戳
				const lastVisitTime = uni.getStorageSync('lastVisitTime');
				// 从localStorage读取剩余体验次数
				this.quotaLeft = uni.getStorageSync('quotaLeft') || this.dailyQuota;

				// 如果本地存储中没有剩余体验次数的信息或为0，初始化为每天的初始值
				if (this.quotaLeft === null || this.quotaLeft === 0) {
					this.quotaLeft = this.dailyQuota;
				}

				// 获取当前时间的时间戳
				const now = new Date().getTime();

				// 如果本地存储中没有记录上一次访问的时间戳或已过期
				if (!lastVisitTime || now - parseInt(lastVisitTime) >= 24 * 60 * 60 * 1000) {
					// 重置体验次数
					this.resetQuota();
					// 更新上一次访问的时间戳为当前时间
					uni.setStorageSync('lastVisitTime', now);
				}
				// else {
				// 	// 如果不是新的一天，检查并递减体验次数
				// 	if (this.quotaLeft > 0) {
				// 		this.quotaLeft--;
				// 		// 更新剩余体验次数到localStorag
				// 		uni.setStorageSync('quotaLeft', this.quotaLeft);
				// 	}
				// }
			},
			// 重置体验次数到每天的初始值
			resetQuota() {
				this.quotaLeft = this.dailyQuota;
				// 将重置后的体验次数存入localStorage
				uni.setStorageSync('quotaLeft', this.quotaLeft);
			},
			async checkReg() {
				let isReg = await wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'isregist'
					}
				}).then(res => {
					uni.hideLoading()
					return res.result
				})
				if (isReg.isregist) {
					// console.log("isReg:", isReg)
					let localopenid = uni.getStorageSync('openid')
					let userinfo = uni.getStorageSync("userinfo")
					uni.setStorageSync('noReg', true)
					// console.log("openid:", localopenid)
					if (!localopenid || !userinfo) {
						uni.setStorageSync('openid', isReg.openid)
						uni.setStorageSync('userinfo', isReg.data.data[0])
					}
				} else {
					// uni.navigateTo({
					// 	url: "/subpack/regist/regist"
					// })
					uni.setStorageSync('noReg', false)
				}
			}
		},
		components: {
			local,
			checktab,
			recommend
		}
	};
</script>

<style scoped lang="scss">
	.content {
		text-align: center;
		height: 400rpx;

		.wrap {
			// color: $uni-color-primary;
			background-color: $uni-bg-color-grey;
			padding: 0 15rpx;

			.line {
				border: 1rpx solid $uni-text-color-grey;
			}
		}

		.swiper {
			height: 450rpx;
		}

		.swiper-item {
			display: block;
			line-height: 300rpx;
			text-align: center;

			// background-color: red;
			image {
				width: 100%;
			}
		}
	}
</style>